﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class publish : System.Web.UI.Page
{
    protected BLL.LostAndFound blllostandfound = new BLL.LostAndFound();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!common.isLoginOrNot())
            {
                MessageBox.Show(this, "请先登录");
                MessageBox.script(Page, "window.location='index.aspx';");
            }
        }
    }

    #region 发布信息
    /// <summary>
    /// 发布信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnPublish_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && checkVcode())
        {
            MODEL.LostAndFound model = new MODEL.LostAndFound();
            model.UserId = ((MODEL.Users)Session["currentUser"]).UId;
            if (txtaGsAttr.Value.Length > 100)
            {
                MessageBox.Show(this, "物品特征字数不能超过100字");
                return;
            }
            if (rdTypeF.Checked)
            {
                model.LostProperty = "寻物启事";
            }
            else
            {
                model.LostProperty = "招领启事";
            }
            model.GoodsName = txtGsName.Value;
            model.Feature = txtaGsAttr.Value;
            model.Address = txtAddress.Value;
            model.Time = Convert.ToDateTime(txtTime.Value);
            //图片
            #region  图片
            if (FUImg.HasFile)
            {
                //判断图片大小，不能超过500kb
                if (FUImg.PostedFile.ContentLength < 500 * 1024)
                {
                    //判断文件的后缀名，是否为图片
                    if (System.IO.Path.GetExtension(FUImg.FileName) == ".jpg" || System.IO.Path.GetExtension(FUImg.FileName) == ".gif" || System.IO.Path.GetExtension(FUImg.FileName) == ".png" || System.IO.Path.GetExtension(FUImg.FileName) == ".jpeg")
                    {
                        //model.Image = FUImg.FileName;
                        //string savePath = Server.MapPath("imgpublish/" + FUImg.FileName);//考虑到图片冲突的问题  所以不能用这方法
                        string s = Guid.NewGuid().ToString();
                        model.Image = s + System.IO.Path.GetExtension(FUImg.FileName);
                        string savePath = Server.MapPath("imgpublish/" + s+ System.IO.Path.GetExtension(FUImg.FileName));
                        FUImg.SaveAs(savePath);
                    }
                    else
                    {
                        model.Image = "noimg.png";
                    }
                }
                else
                {
                    model.Image = "noimg.png";
                }
            }
            else
            {
                model.Image = "noimg.png";
            } 
            #endregion
            
            int lid;
            if (blllostandfound.publishInfo(model,out lid))
            {
                MessageBox.Show(this, "信息发布成功");
                //发布信息成功后，跳转到具体信息展示页面
                MessageBox.script(this, "window.location='detail.aspx?id=" + lid.ToString() + "';");
            }
            else
            {
                MessageBox.Show(this, "信息发布失败，请重新发布");
            }
        }
    }
    #endregion

    #region 检查验证码是否正确
    /// <summary>
    /// 检查验证码是否正确
    /// </summary>
    /// <returns></returns>
    private bool checkVcode()
    {
        if (Session["vCode"] != null)
        {
            if (txtVCode.Value.Trim() == Session["vCode"].ToString())
            {
                return true;
            }
            else
            {
                MessageBox.Show(this, "验证码错误");
                return false;
            }
        }
        else
        {
            MessageBox.Show(this, "验证码过期了");
            return false;
        }
    }
    #endregion
}